package com.gee.spring.boot.rpf.util;

import com.gee.spring.boot.rpf.common.constant.ClientConstant;
import org.springframework.util.StringUtils;

import java.util.Optional;
import java.util.StringJoiner;

/**
 * desc:
 *
 * @author gee wrote on  2021-02-03 14:13:07
 */
public class CommonUtil {

    public static String buildRequestCacheKey(Object ...keys){
        if (keys.length == 0) {
            return null;
        }
        StringJoiner cacheKeyJoiner = new StringJoiner(ClientConstant.CLIENT_REQUEST_CACHE_KEY_SEPARATOR);
        for (Object key : keys) {
            Optional.ofNullable(key).ifPresent(keyObj -> {cacheKeyJoiner.add(keyObj.toString());});
        }
        return cacheKeyJoiner.toString();
    }

    public static String buildRedisCacheKey(Object ...keys){
        if (keys.length == 0) {
            return null;
        }
        StringJoiner cacheKeyJoiner = new StringJoiner(ClientConstant.CLIENT_REDIS_CACHE_KEY_SEPARATOR);
        for (Object key : keys) {
            Optional.ofNullable(key).ifPresent(keyObj -> {cacheKeyJoiner.add(keyObj.toString());});
        }
        return cacheKeyJoiner.toString();
    }

    public static String buildRedisKeyPattern(Object key){
        if (StringUtils.isEmpty(key)) {
            return null;
        }
        return key.toString() + ClientConstant.CLIENT_REDIS_KEY_PATTERN;
    }

    public static Class getJavaType(Integer javaTypeIndex) {
        switch (javaTypeIndex){
            case 1:
                return String.class;
            case 2:
                return Boolean.class;
            case 3:
                return Integer.class;
            case 4:
                return Double.class;
            default:
                return Object.class;
        }
    }


}
